
$().ready(function() {
	validateRule();
});
$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});

function save() {
	var mobile = $.trim($('#mobile').val());
	if (!(/^1(3|4|5|7|8)\d{9}$/.test(mobile))) {
		parent.layer.msg("手机号码有误，请重填");
		return;
	}

	var mid = $('#mid').val();
	if (!mid || mid == "") {
		parent.layer.msg("请绑定会员信息");
		return;
	}

	var cardNo = $('#cardNo').val();
    if (!cardNo || cardNo == "" ) {
        parent.layer.msg("请输入银行卡号");
        return;
    }

    var cardId = $('#cardId').val();
    if (!cardId || cardId=="") {
        parent.layer.msg("请输入身份证号码");
        return;
    }

	var tbCode = $('#tbCode').val();
    if (!tbCode || tbCode=="") {
        parent.layer.msg("请选择银行");
        return;
    }

      var bankCode = "";
	  if(cardNo!="" && cardNo!=null){
		  $.ajax(
				  {
			  url: "https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardBinCheck=true&cardNo="+cardNo,
			  success: function(res){
				  if (res.validated) {
					  bankCode = res.bank;
					  $("#bankId").val(bankCode);

					  $.ajax({
						cache : true,
						type : "POST",
						url : "/mall/supply/update",
						data : $('#signupForm').serialize(),// 你的formid
						async : false,
						error : function(request) {
							parent.layer.alert("Connection error");
						},
						success : function(data) {
							if (data.code == 0) {
								parent.layer.msg("操作成功");
								parent.reLoad();
								var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
								parent.layer.close(index);
							} else {
								if (data.msg) {
									parent.layer.alert(data.msg)
								}else {
									window.location.href = "/";
								}
							}

						}
					});
					} else {
						parent.layer.msg("验证银行卡号错误，请核对后重试");
						return;
					}
		  }});
	  }else{
		  $.ajax({
		  		cache : true,
		  		type : "POST",
		  		url : "/mall/supply/update",
		  		data : $('#signupForm').serialize(),// 你的formid
		  		async : false,
		  		error : function(request) {
		  			parent.layer.alert("Connection error");
		  		},
		  		success : function(data) {
		  			if (data.code == 0) {
		  				parent.layer.msg("操作成功");
		  				parent.reLoad();
		  				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
		  				parent.layer.close(index);
		  			} else {
		  				if (data.msg) {
		  					parent.layer.alert(data.msg)
		  				}else {
		  					window.location.href = "/";
		  				}
		  			}

		  		}
		  	});
	  }
	/*var card = $("#cardId").val();
	if (card == "" || card == null) {
		parent.layer.msg("身份证不能为空");
		return;
	}
	// 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X
	var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
	if (reg.test(card) === false) {
		parent.layer.msg("身份证输入不合法");
		$("#cardId").val("");
		return;
	}
	var isGong=$('input:radio[name="isGong"]:checked').val();
	if (isGong == "" || isGong == null) {
		parent.layer.msg("账户类型必选");
		return;
	}
	if(isGong==1){
		var bbCode = $("#bbCode").val();
		if (bbCode == "" || bbCode == null) {
			parent.layer.msg("支行银联号不能为空");
			return;
		}
	}*/
	
}

/**
 * 关联会员
 */
function relateMember() {
	// var centreId = $("*[name='centreId']").val();
	layer.open({
		id:"relation_member",
		type:2,
		title:"绑定会员" ,
		maxmin: true,
		shadeClose: true,
		area : [ '70%', '80%' ],
		content:"/mall/supply/relateMember?type=gys"
	})

}
/**
 * 关联买手
 */
function relateMember1() {
	// var centreId = $("*[name='centreId']").val();
	layer.open({
		id:"relation_member",
		type:2,
		title:"关联买手" ,
		maxmin: true,
		shadeClose: true,
		area : [ '70%', '80%' ],
		content:"/mall/supply/relateMember?type=ms"
	})

}
/**
 * 设置买手信息
 * @param memberId
 * @param memberName
 */
function loadMemberInfo1(memberId, memberName) {
    $('#buymid').val(memberId)
    $('#buyrealnm').val(memberName)
}
/**
 * 图片输入框失去焦点
 */
function onThumbBlur() {
	var url = $.trim($('#picUrl').val());
	if (url) {
		if (url.indexOf("http") >= 0 || url.indexOf("https") >= 0) {
			$('#thumbImg').attr('src', url);
		}else {
			var prefix = "http://pic.jinzhishenghuo.com";
			$('#thumbImg').attr('src', prefix + url);
		}
	}


}

/**
 * 顶级分类变更
 * @param pcate
 */
function onPcateChange(pcate) {
	var parentId = pcate.value;
	$.ajax({
		cache: true,
		type: "POST",
		url: "/category/manage/ccates",
		data: {
			parentId: parentId
		},
		async: false,
		error: function (request) {
			parent.layer.alert("Connection error");
		},
		success: function (data) {
			if (data.code == 0) {
				var ccates = data.ccates;
				if (ccates && ccates.length > 0) {
					$('#ccate').empty();
					$('#ccate').append("<option value=>" + "选择下级分类" + "</option>");
					for (var i = 0; i < ccates.length; i++) {
						var item = ccates[i];
						$('#ccate').append("<option value='" + item.id + "'>" + item.name + "</option>");
					}
				}


			} else {
				if (data.msg) {
					parent.layer.alert(data.msg)
				} else {
					window.location.href = "/";
				}
			}

		}
	});
}


function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			},
			supplycode : {
				required : true
			},
			linkman : {
				required : true
			},
			mobile : {
				required : true
			},
			mid : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "供应商名称不能为空"
			},
			supplycode : {
				required : icon + "供应商编码不能为空"
			},
			linkman : {
				required : icon + "联系人不能为空"
			},
			mobile : {
				required : icon + "联系人电话不能为空"
			},
			mid : {
				required : icon + "绑定会员不能为空"
			}
		}
	})
}

/**
 * 加载图片URL
 * @param inputId
 * @param imgId
 * @param url
 */
function loadImgUrl(inputId, imgId, url) {
	$('#'+inputId +'').val(url);
	$('#'+imgId +'').attr('src', url);
}

/**
 * 选择图片页面
 * @param inputId
 * @param imgId
 */
function onSelectImgBtnClick(inputId, imgId) {
	layer.open({
		id:'selectImg',
		type : 2,
		title : '选择图片',
		maxmin : true,
		shadeClose : false,
		area : [ '60%', '70%' ],
		content : '/platform/config/selectImg/' + inputId +"/"+ imgId // iframe的url
	});
}

/**
 * 设置会员信息
 * @param memberId
 * @param memberName
 */
function loadMemberInfo(memberId,memberName) {
	$('#mid').val(memberId)
	$('#username').val(memberName)
}